package com.dx.wx.service.impl;

import com.dx.wx.service.DxHomeService;
import com.dx.wx.service.DxOrderService;
import com.dx.wx.service.DxUserService;
import com.dx.wx.util.DxUtil;
import com.ejlchina.data.Array;
import com.ejlchina.data.Mapper;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class DxOrderServiceImpl implements DxOrderService {

    @Autowired
    private DxUserService dxUserService;

    @Autowired
    private DxHomeService dxHomeService;


    @Override
    public void save(Long phone,Map data) {
        Mapper mapper = dxUserService.findByMobile(String.valueOf(phone));
//        String openUserId = DxUtil.getOpenUserId(mapper.getString("field_5h7D5__c"));
        if(data.get("record_type").equals("record_FL122__c")){//晒单有礼 设置价格
            Array array = findByType("record_FL122__c",String.valueOf(phone));
            if(array.size()>0){
                throw new RRException("请勿重复提交!");
            }
            Mapper gift = dxHomeService.gift();
            data.put("order_amount",gift.getDouble("field_A416s__c"));
        }
        String openUserId = mapper.getArray("owner").getString(0);
        data.put("user_id__c",mapper.getString("_id"));//晒单人
        data.put("owner", Arrays.asList(openUserId));
        Map res = DxUtil.createMergeData(data);
        Map details = new HashMap();
        details.put("SalesOrderProductObj",data.get("details"));
        res.put("details",details);
        res.put("dataObjectApiName","SalesOrderObj");
        DxUtil.save(res);
    }

    @Override
    public void saveCus(Long phone, Map data) {
        //检查防伪码是否有
        String code = (String) data.get("sec_code__c");
        if(code==null||code.equals("")){
            throw new RRException("请填写防伪码");
        }
        Map filter = new HashMap();
        filter.put("field_name","sec_code__c");
        filter.put("field_values",code);
        filter.put("operator","EQ");
        Map res = DxUtil.createSearchData(filter);
        res.put("dataObjectApiName","sec_code__c");
        Mapper codeMapper = DxUtil.findOne(res);
        if(codeMapper==null){
            throw new RRException("请检查防伪码是否正确");
        }
        Mapper mapper = dxUserService.findByMobile(String.valueOf(phone));
        String openUserId = DxUtil.getAdminOpenUserId();
        data.put("owner", Arrays.asList(openUserId));
        data.put("user__c",mapper.getString("_id"));
        data.put("point__c",codeMapper.getDouble("point__c"));
        data.put("status__c",0);
        data.put("field_Oi2V9__c",data.get("sec_code__c"));
        res = DxUtil.createMergeData(data);
        res.put("dataObjectApiName","cus_order__c");
        DxUtil.save(res);
    }

    @Override
    public Array findByPhone(Long phone, Long startDate, Long endDate, Integer type) {

        Map filter = new HashMap();
        if(type==0){ //自己手机号查询
            filter.put("field_name","field_r1V1t__c");
        }else { //上级手机号查询
            filter.put("field_name","top_user_phone__c");
        }
        filter.put("field_values",Arrays.asList(phone));
        filter.put("operator","EQ");
        Map dateStartFilter = new HashMap();
        Map dateEndFilter = new HashMap();
        if(startDate!=null&&endDate!=null){
            //传入时间
            dateStartFilter.put("field_name","order_time");
            dateStartFilter.put("field_values",Arrays.asList(startDate));
            dateStartFilter.put("operator","GTE");//大于等于
            dateEndFilter.put("field_name","order_time");
            dateEndFilter.put("field_values",Arrays.asList(endDate));
            dateEndFilter.put("operator","LTE");//小于等于
        }
        Map data = DxUtil.createSearchData(filter,dateStartFilter,dateEndFilter);
        data.put("dataObjectApiName","SalesOrderObj");
        return DxUtil.findSimple(data);
    }

    @Override
    public Array findByType(String type,String phone) {
        Map filter = new HashMap();
        filter.put("field_name","record_type");
        filter.put("field_values",type);
        filter.put("operator","EQ");
        Map filterPhone = new HashMap();
        filterPhone.put("field_name","field_r1V1t__c");
        filterPhone.put("field_values",phone);
        filterPhone.put("operator","EQ");
        Map data = DxUtil.createSearchData(filter,filterPhone);
        data.put("dataObjectApiName","SalesOrderObj");
        return DxUtil.findSimple(data);
    }

    @Override
    public Array findCusByPhone(Long phone, Long startDate, Long endDate, Integer type) {
        Mapper mapper = dxUserService.findByMobile(String.valueOf(phone));
        Map filter = new HashMap();
        filter.put("field_name","user__c");
        filter.put("field_values",mapper.getString("_id"));
        filter.put("operator","EQ");
        Map dateStartFilter = new HashMap();
        Map dateEndFilter = new HashMap();
        Map data;
        if(startDate!=null&&endDate!=null){
            //传入时间
            dateStartFilter.put("field_name","create_time");
            dateStartFilter.put("field_values",Arrays.asList(startDate));
            dateStartFilter.put("operator","GTE");//大于等于
            dateEndFilter.put("field_name","create_time");
            dateEndFilter.put("field_values",Arrays.asList(endDate));
            dateEndFilter.put("operator","LTE");//小于等于
           data = DxUtil.createSearchData(filter,dateStartFilter,dateEndFilter);
        }else {
            data = DxUtil.createSearchData(filter);
        }
        data.put("dataObjectApiName","cus_order__c");
        return DxUtil.findSimple(data);
    }


    @Override
    public Array findListByPhone(Long phone, Long startDate, Long endDate, Integer type) {

        Map filter = new HashMap();
        if(type==0){ //自己手机号查询
            filter.put("field_name","field_S0fZd__c");
        }else if (type==1){
            //负责人手机号
//            filter.put("field_name","field_O01vn__c");
            filter.put("field_name","field_qhv81__c");
        }else {
            //主管手机号
            filter.put("field_name","field_21X02__c");
        }
        filter.put("field_values",Arrays.asList(phone));
        filter.put("operator","EQ");

        Map data ;
        Map order = new HashMap();
        order.put("fieldName","field_k8f95__c");
        order.put("isAsc",false);
        if(startDate==null){
            //两个月前
            startDate = System.currentTimeMillis()-60*60*24*30*1*1000L;
            endDate = System.currentTimeMillis();
        }
        if(startDate!=null&&endDate!=null){
            Map dateStartFilter = new HashMap();
            Map dateEndFilter = new HashMap();
            //传入时间
            dateStartFilter.put("field_name","field_k8f95__c");
            dateStartFilter.put("field_values",Arrays.asList(startDate));
            dateStartFilter.put("operator","GTE");//大于等于
            dateEndFilter.put("field_name","field_k8f95__c");
            dateEndFilter.put("field_values",Arrays.asList(endDate));
            dateEndFilter.put("operator","LTE");//小于等于s
             data = DxUtil.createSearchData(Arrays.asList(filter,dateStartFilter,dateEndFilter),Arrays.asList(order));
        }else {
            data = DxUtil.createSearchData(Arrays.asList(filter),Arrays.asList(order));
        }
        //本身就是时间倒叙的

        data.put("dataObjectApiName","SalesOrderProductObj");
        return DxUtil.findSimple(data);
    }

    @Override
    public Array findDetailByOrderId(String orderId) {
        Map filter = new HashMap();
        filter.put("field_name","order_id");
        filter.put("field_values",Arrays.asList(orderId));
        filter.put("operator","EQ");
        Map data = DxUtil.createSearchData(filter);
        data.put("dataObjectApiName","SalesOrderProductObj");
        return DxUtil.findSimple(data);
    }

    @Override
    public Mapper findByOrderNo(String orderNo) {
        Map filter = new HashMap();
        filter.put("field_name","name");
        filter.put("field_values",Arrays.asList(orderNo));
        filter.put("operator","EQ");
        Map data = DxUtil.createSearchData(filter);
        data.put("dataObjectApiName","SalesOrderObj");
        return DxUtil.findOne(data);
    }
}
